package 反射;

//通过反射获得泛型的实际类型参数

import org.junit.Test;
import org.junit.runners.Parameterized;

import javax.management.Attribute;
import javax.xml.soap.Detail;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class reflect_test {
    @Test
   public void test1() throws NoSuchMethodException {
       List<Book> list = new ArrayList<Book>();
       //利用反射拿到list的实际类型参数
       Method test8_method = reflect_test.class.getMethod("test8", List.class);
       //获得泛型类型参数
       Type[] types = test8_method.getGenericParameterTypes();
        ParameterizedType pt = (ParameterizedType) types[0];
        System.out.println(pt);
        //获得原始类型
        System.out.println(pt.getRawType());
        //获得实际参数类型，实际参数类型也是一个数组，比如Map<String,String>
        //这里只有一个参数,无需遍历
        System.out.println(pt.getActualTypeArguments()[0]);


    }
   public void test8(List<Book> list)
   {

   }

}

class Book{

    private int id;
    private String name;
    private int price;
    private String author;
    private Detail detail;
    private Attribute attribute;

    public Attribute getAttribute() {
        return attribute;
    }
    public void setAttribute(Attribute attribute) {
        this.attribute = attribute;
    }
    public Detail getDetail() {
        return detail;
    }
    public void setDetail(Detail detail) {
        this.detail = detail;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Book(String name, String author) {
        this.name = name;
        this.author = author;
    }
    public Book() {
    }

}
